const vue = require("rollup-plugin-vue");
const { terser } = require("rollup-plugin-terser");
const glob = require("glob");
const path = require("path");

const babel = require("@rollup/plugin-babel")

const files = glob.sync(path.join(__dirname, "./src/components/**/*.js"));
function getConfig(input, name, format = "umd") {
  let dirName = format == "umd" ? "lib" : "es";
  return {
    input,
    output: [
      {
        file: `${dirName}/${name.toLowerCase()}.js`,
        format,
        name: "kay",
      },
    ],
    plugins: [
      vue({
        css: true,
        compileTemplate: true,
      }),
      terser(),
      babel({
        babelHelpers:"runtime",
        exclude: "**/node_modules/**",
        presets: ["@vue/babel-preset-jsx"]
      }),
    ],
  };
}

const getComponent = (format) => {
  console.log(files);
  return files.map((item) => {
    let nameArr = item.split("/");
    let name = nameArr[nameArr.length - 2];
    if(name.startsWith("_")){
      return null
    }
    return getConfig(item, name,format);
  }).filter(Boolean)
}
// lib打包跟es打包
const components = [...getComponent(),...getComponent('es')];

// 全局打包，可部署cdn的js
components.push(getConfig("./src/index.js", "index"));

module.exports = components;

